#!/bin/sh

#%Module
#% description: Displays and overlays raster map layers in the active display frame on the graphics monitor
#% keywords: display
#%End
#%flag
#% key: i
#% description: Invert catlist
#%End
#%Option
#% key: map
#% type: string
#% required: yes
#% multiple: no
#% description: Raster map to be displayed
#% gisprompt: old,cell,raster
#%End
#%Option
#% key: catlist
#% type: string
#% required: no
#% multiple: yes
#% key_desc: cat[-cat]
#% description: List of categories to be displayed (INT maps)
#%End
#%Option
#% key: vallist
#% type: string
#% required: no
#% multiple: yes
#% key_desc: val[-val]
#% description: List of values to be displayed (FP maps)
#%End
#%Option
#% key: opacity
#% type: string
#% required: no
#% multiple: no
#% key_desc: val[-val]
#% description: Set opacity between 0-100%
#%End


if [ -z "$GISBASE" ] ; then
    echo "You must be in GRASS GIS to run this program." 1>&2
    exit 1
fi

if [ "$1" != "@ARGS_PARSED@" ] ; then
    exec g.parser "$0" "$@"
fi

# CODE GOES HERE

cmdfile="`g.gisenv get=GRASS_PYCMDFILE`"

if [ -e ${cmdfile} ] && [ -n "${cmdfile}" ]; then
    echo -n
else
    g.message -e "GRASS_PYCMDFILE File not found. Run p.mon"
    exit 1
fi


eval "`echo ${GIS_OPT_MAP}| sed -e 's/^/NAME=/' -e 's/@/; MAPSET=/'`;"

#echo $NAME 

if [ -z "${MAPSET}" ]; then
    mapset=""
else
    mapset="mapset=${MAPSET}"
fi

#echo $MAPSET

eval "`g.findfile element=cell file=${NAME} ${mapset}`"

#echo $NAME 
#echo $MAPSET

if [ -z "${GIS_OPT_CATLIST}" ]; then
    CATLIST="None"
else
    CATLIST="${GIS_OPT_CATLIST}"
fi

if [ -z "${GIS_OPT_VALLIST}" ]; then
    VALLIST="None"
else
    VALLIST="${GIS_OPT_VALLIST}"
fi

if [ -z "${GIS_OPT_OPACITY}" ]; then
    OPACITY="100"
else
    OPACITY="${GIS_OPT_OPACITY}"
fi

if [ "${GIS_FLAG_I}" -eq "1" ]; then
    INVERT="True"
else
    INVERT="False"
fi


#cmd="self.map.AddRasterLayer(self, $NAME $MAPSET $CATLIST $VALLIST $INVERCATS $INVERT $OPACITY)"

cmd="addraster ${name} ${mapset} ${CATLIST} ${VALLIST} ${INVERT} ${OPACITY}"

#echo ${cmd}
echo ${cmd} >>${cmdfile}
